﻿using BLL.AntiFake;
using BLL.Origins;
using Common;
using Model;
using Model.AntiFake;
using Model.Origins;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Origins;
namespace DAGApp
{
    public partial class OriginsCode : System.Web.UI.Page
    {
        OriginsTempletBLL oribll = new OriginsTempletBLL();
        QueryCodeLogBLL bll = new QueryCodeLogBLL();
        QueryLogBLL bll0 = new QueryLogBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string fw_code = Request.QueryString["code"];//获取url路径的防伪码信息
                if (!string.IsNullOrEmpty(fw_code))
                {//不为空

                    string TableName;
                   
                    bool flag = bll.TabIsExist(fw_code, out TableName);
                   
                    if (flag)
                    {//表存在
                        //查询真伪
                        AntiFake_DigitcodeModel DigitcodeModel = bll.GetModelByFw_Code(fw_code);

                        string msg;
                            if (DigitcodeModel != null && !string.IsNullOrEmpty(DigitcodeModel.Wl_Code))
                            {
                                msg = "正品！";
                                //生成记录
                                QueryLogModel model = new QueryLogModel();
                                IPMethod me = new IPMethod();
                                model.WlCode = DigitcodeModel.Wl_Code;
                                model.QueryType = "4";//查询方式
                                model.QueryValue = me.GetHostAddress();
                                model.QueryTime = DateTime.Now;
                                model.QueryAddress = me.IPGetCity(model.QueryValue);
                                model.Valid = 1;
                                //  model.NickName = "遥远的她";
                                // model.Status = 0;//关注状态 0 表示已关注 1 表示未关注
                                model.QueryMessage = msg;
                                ResultEntity entity = bll0.AddQueryLog(model);
                                //显示对应的模板信息
                                OriginsTempletModel tempModel = oribll.GetModelById(DigitcodeModel.TempletId);
                                this.ResultContents.InnerHtml = tempModel.Content;
                            }
                            else
                            {
                                msg = "该防伪码不存在对应的溯源码";
                                //生成记录
                                QueryLogModel model = new QueryLogModel();
                                IPMethod me = new IPMethod();
                                model.WlCode = DigitcodeModel.Wl_Code;
                                model.QueryType = "4";//查询方式
                                model.QueryValue = me.GetHostAddress();
                                model.QueryTime = DateTime.Now;
                                model.QueryAddress = me.IPGetCity(model.QueryValue);
                                model.Valid = 1;
                              
                              //  model.NickName = "遥远的她";
                               // model.Status = 0;//关注状态 0 表示已关注 1 表示未关注
                                model.QueryMessage = msg;
                                ResultEntity entity = bll0.AddQueryLog(model);
                                System.Web.HttpContext.Current.Response.Write(String.Format("<script language=\"javascript\">alert(\"{0}\");window.location.replace(\"{1}\")</script>", "该防伪码不存在对应的溯源码！", "Index.aspx?code=" + fw_code));
                            }
                       

                    }
                    else//表不存在
                        System.Web.HttpContext.Current.Response.Write(String.Format("<script language=\"javascript\">alert(\"{0}\");window.location.replace(\"{1}\")</script>", "该防伪码不存在对应的溯源码！", "Index.aspx?code="+fw_code));

                }
              
            }
        }
    }
}